/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
import React, { Component } from "react";
import { Platform,Text } from "react-native";
import { Recognizer, Synthesizer,  } from "react-native-speech-iflytek";

export default class App extends Component {
    constructor(props) {
        super(props);

        if (Platform.OS === 'android') {
            Synthesizer.init("5c14cdf5");
            Recognizer.init("5c14cdf5");
        } else if (Platform.OS === 'ios') {
            Synthesizer.init("5c14cdf5");
            Recognizer.init("5c14cdf5");
        }

        this.state = {
            text: "你可以使用这个组件进行网页的来回导航，并且为网页内容设置多方面的属性。",
            recordBtnText: "Press to record"
        };
        this.onRecordStart = this.onRecordStart.bind(this);
        this.onRecordEnd = this.onRecordEnd.bind(this);
        this.onRecordCancel = this.onRecordCancel.bind(this);
        this.onRecognizerResult = this.onRecognizerResult.bind(this);
        this.onRecognizerError = this.onRecognizerError.bind(this);
        this.onRecognizerVolumeChanged = this.onRecognizerVolumeChanged.bind(this);
        this.onSyntheBtnPress = this.onSyntheBtnPress.bind(this);
        this.onIsSpeakingBtnPress = this.onIsSpeakingBtnPress.bind(this);
        this.onResumeBtnPress = this.onResumeBtnPress.bind(this);
    }
    componentWillUnmount(): void {
        this.onPauseBtnPress();
        console.log("执行了+componentWillUnmount");
    }

    render() {
        return <Text>

        </Text>
    }

    onRecordStart() {
        this.setState({ recordBtnText: "Release to stop" });

        Recognizer.start();
    }

    onRecordEnd() {
        this.setState({ recordBtnText: "Press to record" });
        Recognizer.stop();
    }

    onRecordCancel(evt) {
        // setTimeout(() => {
        //   Recognizer.cancel();
        // }, 500);
    }

    onRecognizerResult(e) {
        if (!e.isLast) {
            return;
        }
        this.setState({ text: e.result });
    }

    onRecognizerError(result) {
        if (result.errorCode !== 0) {
            alert(JSON.stringify(result));
        }
    }

    onRecognizerVolumeChanged() {

    }

    async onSyntheBtnPress() {
        const { content } = this.props;
        Synthesizer.start(content);
    }

    async onPauseBtnPress() {
        Synthesizer.pause();
    }

    onResumeBtnPress() {
        Synthesizer.resume();
    }

    async onIsSpeakingBtnPress() {
        let isSpeaking = await Synthesizer.isSpeaking();
        // alert(isSpeaking);
        return isSpeaking
    }

    onSynthesizerSpeakCompletedEvent(result) {
        // alert('onSynthesizerSpeakCompletedEvent\n\n' + JSON.stringify(result));
        // 读取完毕的
    }

    onSynthesizerBufferCompletedEvent(result) {
        // alert('onSynthesizerBufferCompletedEvent\n\n' + JSON.stringify(result));
    }
}